<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人主页</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        :root {
            --primary-color: #ffd700;
            --primary-light: #fff9e6;
            --text-color: #333;
            --border-color: #ffe066;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background-color: var(--primary-light);
        }

        .container {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 15px;
        }

        .profile-header {
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            gap: 30px;
            margin-bottom: 20px;
        }

        .avatar {
            width: 120px;
            height: 120px;
            border-radius: 60px;
            object-fit: cover;
            border: 3px solid var(--primary-color);
        }

        .user-info {
            flex: 1;
            position: relative;
        }

        .user-name {
            font-size: 24px;
            color: var(--text-color);
            margin-bottom: 10px;
        }

        .credit-score {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 15px;
        }

        .credit-badge {
            background: var(--primary-light);
            padding: 5px 15px;
            border-radius: 20px;
            color: var(--text-color);
            font-weight: 500;
            border: 1px solid var(--border-color);
        }

        .credit-stars {
            color: var(--primary-color);
            display: inline-flex;
            gap: 5px;
        }

        .credit-stars i {
            font-size: 20px;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .credit-stars i:hover {
            transform: scale(1.2);
            color: #ffd700;
            text-shadow: 0 0 5px rgba(255, 215, 0, 0.5);
        }

        .credit-stars i:hover ~ i {
            color: #ddd;
            transform: scale(1);
            text-shadow: none;
        }

        .user-stats {
            display: flex;
            gap: 30px;
        }

        .stat-item {
            text-align: center;
        }

        .stat-value {
            font-size: 20px;
            font-weight: bold;
            color: var(--text-color);
        }

        .stat-label {
            font-size: 14px;
            color: #666;
        }

        .content-section {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        .section-title {
            font-size: 18px;
            color: var(--text-color);
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid var(--border-color);
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .product-type-switch {
            display: flex;
            gap: 15px;
            align-items: center;
        }

        .type-option {
            font-size: 16px;
            color: #666;
            cursor: pointer;
            padding: 5px 10px;
            border-radius: 4px;
            transition: all 0.3s;
        }

        .type-option:hover {
            color: var(--text-color);
        }

        .type-option.active {
            color: var(--text-color);
            font-weight: 500;
            background: var(--primary-light);
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
        }

        .product-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            transition: all 0.3s ease;
            border: 1px solid var(--border-color);
            position: relative;
            cursor: pointer;
        }

        .product-type-tag {
            position: absolute;
            top: 8px;
            left: 8px;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
            color: white;
            z-index: 1;
        }

        .auction-tag {
            background: rgba(255, 68, 0, 0.8);
        }

        .auction-ongoing-tag {
            background: rgba(82, 196, 26, 0.8);
        }

        .auction-upcoming-tag {
            background: rgba(255, 68, 0, 0.8);
        }

        .normal-tag {
            background: rgba(82, 196, 26, 0.8);
        }

        .auction-info {
            font-size: 12px;
            color: #ff4400;
            margin-top: 4px;
        }

        .min-increment {
            color: #666;
            font-size: 12px;
            margin-left: 8px;
        }

        .chat-button {
            position: absolute;
            top: 0;
            right: 0;
            padding: 8px 16px;
            background: var(--primary-color);
            border: none;
            border-radius: 20px;
            color: var(--text-color);
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s;
            z-index: 2;
        }

        .chat-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .chat-button:disabled {
            background: #ddd;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .product-card:hover::after {
            content: "点击查看详情";
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(255, 215, 0, 0.9);
            color: #333;
            padding: 8px;
            text-align: center;
            font-size: 14px;
            transform: translateY(0);
            transition: transform 0.3s ease;
        }

        .product-card:hover .product-info {
            transform: translateY(-30px);
            transition: transform 0.3s ease;
        }

        .product-image {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }

        .product-info {
            padding: 10px;
        }

        .product-name {
            font-size: 14px;
            color: var(--text-color);
            margin-bottom: 5px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .product-price {
            color: #ff4400;
            font-weight: bold;
        }

        .reviews-list {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .review-item {
            padding: 15px;
            border-radius: 8px;
            background: var(--primary-light);
            border: 1px solid var(--border-color);
        }

        .review-header {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid var(--border-color);
        }

        .review-pair {
            display: flex;
            gap: 20px;
            margin-top: 15px;
        }

        .buyer-review, .seller-review {
            flex: 1;
            padding: 15px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .seller-review {
            background: #f8f9fa;
        }

        .review-title {
            font-weight: bold;
            color: #666;
            margin-bottom: 10px;
            padding-bottom: 5px;
            border-bottom: 1px solid #eee;
        }

        .reviewer-info {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .reviewer-avatar {
            width: 40px;
            height: 40px;
            border-radius: 20px;
            object-fit: cover;
        }

        .reviewer-name {
            font-weight: 500;
            color: var(--text-color);
        }

        .review-date {
            color: #666;
            font-size: 12px;
        }

        .review-rating {
            color: var(--primary-color);
        }

        .review-content {
            color: var(--text-color);
            line-height: 1.5;
            margin-top: 10px;
        }

        .product-info-header {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 15px;
            padding: 10px;
            background: #fff;
            border-radius: 8px;
            border: 1px solid #eee;
        }

        .product-thumbnail {
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 4px;
        }

        .product-brief {
            flex: 1;
        }

        .product-name {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }

        .product-price {
            color: #ff4400;
        }

        .order-info {
            font-size: 12px;
            color: #666;
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .empty-state i {
            font-size: 48px;
            color: var(--primary-color);
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="profile-header">
            <img src="" alt="用户头像" class="avatar" id="userAvatar">
            <div class="user-info">
                <h1 class="user-name" id="userName"></h1>
                <div class="credit-score">
                    <div class="credit-badge">信誉分: <span id="creditScore"></span></div>
                    <div class="credit-stars" id="creditStars"></div>
                </div>
                <div class="user-stats">
                    <div class="stat-item">
                        <div class="stat-value" id="productCount">0</div>
                        <div class="stat-label">在售商品</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-value" id="soldCount">0</div>
                        <div class="stat-label">已售出</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-value" id="reviewCount">0</div>
                        <div class="stat-label">收到评价</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="content-section">
            <div class="section-title">
                <div class="product-type-switch">
                    <span class="type-option active" data-type="normal">在售商品</span>
                    <span class="type-option" data-type="auction">在售拍卖</span>
                </div>
            </div>
            <div class="product-grid" id="productList">
                <!-- 商品列表将通过JavaScript动态加载 -->
            </div>
        </div>

        <div class="content-section">
            <h2 class="section-title">买家评价</h2>
            <div class="reviews-list" id="reviewsList">
                <!-- 评价列表将通过JavaScript动态加载 -->
            </div>
        </div>
    </div>

    <script>
        let userId = null;

        $(document).ready(function() {
            // 从URL获取用户ID
            const urlParams = new URLSearchParams(window.location.search);
            userId = urlParams.get('id');
            
            if (userId) {
                loadUserProfile();
                loadUserProducts();
                loadUserReviews();
            }
        });

        function loadUserProfile() {
            $.ajax({
                url: `/profile/${userId}`,
                type: 'GET',
                success: function(response) {
                    console.log("用户信息", response);
                    if (response.state === 1) {
                        const user = response.data;
                        $('#userAvatar').attr('src', user.avatar || 'default-avatar.jpg');
                        $('#userName').text(user.nickname);
                        
                        // 检查是否是当前登录用户
                        $.ajax({
                            url: '/user/get-user',
                            type: 'GET',
                            success: function(currentUserResponse) {
                                console.log("当前用户", currentUserResponse);
                                if (currentUserResponse.state === 1 && currentUserResponse.data) {
                                    const currentUser = currentUserResponse.data;
                                    // 如果不是当前用户，显示聊天按钮
                                    if (currentUser.id !== parseInt(userId)) {
                                        $('.user-info').append(`
                                            <button class="chat-button" onclick="startChat(${userId})">
                                                <i class="fas fa-comments"></i> 发起聊天
                                            </button>
                                        `);
                                    }
                                } else {
                                    // 未登录用户查看他人主页时也显示聊天按钮
                                    $('.user-info').append(`
                                        <button class="chat-button" onclick="startChat(${userId})">
                                            <i class="fas fa-comments"></i> 发起聊天
                                        </button>
                                    `);
                                }
                            },
                            error: function() {
                                // 请求失败时也显示聊天按钮，让用户可以尝试发起聊天
                                $('.user-info').append(`
                                    <button class="chat-button" onclick="startChat(${userId})">
                                        <i class="fas fa-comments"></i> 发起聊天
                                    </button>
                                `);
                            }
                        });

                        // 其他个人信息展示代码保持不变
                        const score = parseFloat(user.creditScore);
                        $('#creditScore').text(score.toFixed(1));
                        $('#productCount').text(user.productCount || 0);
                        $('#soldCount').text(user.soldCount || 0);
                        $('#reviewCount').text(user.reviewCount || 0);
                        
                        // 星级评分相关代码保持不变...
                    }
                }
            });
        }

        function loadUserProducts() {
            $.ajax({
                url: `/profile/products/${userId}`,
                type: 'GET',
                success: function(response) {
                    console.log("商品",response);
                    if (response.state === 1) {
                        const products = response.data;
                        if (products && products.length > 0) {
                            window.normalProducts = products.filter(p => p.isAuction !== 1);
                            window.auctionProducts = products.filter(p => p.isAuction === 1);
                            
                            // 默认显示普通商品
                            displayProducts('normal');
                        } else {
                            showEmptyState('productList', '暂无商品');
                        }
                    }
                }
            });
        }

        function displayProducts(type) {
            const productList = $('#productList');
            productList.empty();
            
            const products = type === 'normal' ? window.normalProducts : window.auctionProducts;
            
            if (!products || products.length === 0) {
                showEmptyState('productList', type === 'normal' ? '暂无在售商品' : '暂无在售拍卖');
                return;
            }

            products.forEach(product => {
                const isAuction = type === 'auction';
                const productCard = `
                    <div class="product-card" onclick="location.href='${isAuction ? 'auction' : 'product'}-detail.html?id=${isAuction ? product.auctionId : product.id}'">
                        <div class="product-type-tag ${isAuction ? 
                            (product.auctionStatus === '1' ? 'auction-ongoing-tag' : 'auction-upcoming-tag') : 
                            'normal-tag'}">
                            ${isAuction ? (product.auctionStatus === '1' ? '正在进行' : '即将开始') : '闲置'}
                        </div>
                        <img src="${product.firstImage || 'default-product.jpg'}" 
                             alt="${product.name}" 
                             class="product-image">
                        <div class="product-info">
                            <div class="product-name">${product.name}</div>
                            <div class="price-info">
                                <span class="product-price">¥${parseFloat(product.price).toFixed(2)}</span>
                                ${isAuction ? `<span class="min-increment">加价幅度：¥${product.minIncrement || 0}</span>` : ''}
                            </div>
                            ${isAuction ? `
                            <div class="auction-info">
                                ${product.auctionEndTime ? `结束时间：${new Date(product.auctionEndTime).toLocaleString()}` : ''}
                            </div>` : ''}
                        </div>
                    </div>
                `;
                productList.append(productCard);
            });
        }

        // 绑定切换事件
        $(document).on('click', '.type-option', function() {
            const type = $(this).data('type');
            $('.type-option').removeClass('active');
            $(this).addClass('active');
            displayProducts(type);
        });

        function loadUserReviews() {
            $.ajax({
                url: `/profile/comments/${userId}`,
                type: 'GET',
                success: function(response) {
                    if (response.state === 1) {
                        const reviews = response.data;
                        if (reviews && reviews.length > 0) {
                            const reviewsList = $('#reviewsList');
                            reviewsList.empty();
                            
                            reviews.forEach(review => {
                                const reviewItem = `
                                    <div class="review-item">
                                        <div class="review-header">
                                            <div class="order-info">
                                              
                                                <span style="margin-left: 15px;">交易时间：${new Date(review.createTime).toLocaleString()}</span>
                                            </div>
                                        </div>
                                        <div class="product-info-header">
                                            <img src="${review.firstImage || 'default-product.jpg'}" 
                                                 class="product-thumbnail" 
                                                 alt="${review.productName}">
                                            <div class="product-brief">
                                                <div class="product-name">${review.productName}</div>
                                                <div class="product-price">¥${review.price ? review.price.toFixed(2) : '0.00'}</div>
                                            </div>
                                        </div>
                                        <div class="review-pair">
                                            <div class="buyer-review">
                                                <div class="review-title">买家评价</div>
                                                <div class="reviewer-info">
                                                    <img src="${review.buyerAvatar || 'default-avatar.jpg'}"
                                                         class="reviewer-avatar" 
                                                         alt="${review.buyerName}">
                                                    <div>
                                                        <div class="reviewer-name">${review.buyerName}</div>
                                                        <div class="review-rating">
                                                            ${getStarRating(review.rating)}
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="review-content">${review.content || '暂无评价内容'}</div>
                                                <div class="review-date">评价时间：${new Date(review.createTime).toLocaleString()}</div>
                                            </div>
                                            ${review.sellerContent ? `
                                                <div class="seller-review">
                                                    <div class="review-title">卖家评价</div>
                                                    <div class="reviewer-info">
                                                        <img src="${review.sellerAvatar || 'default-avatar.jpg'}"
                                                             class="reviewer-avatar" 
                                                             alt="卖家">
                                                        <div>
                                                            <div class="reviewer-name">卖家</div>
                                                            <div class="review-rating">
                                                                ${getStarRating(review.sellerRating)}
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="review-content">${review.sellerContent}</div>
                                                    <div class="review-date">评价时间：${new Date(review.updateTime).toLocaleString()}</div>
                                                </div>
                                            ` : `
                                                <div class="seller-review">
                                                    <div class="review-title">卖家评价</div>
                                                    <div class="review-content" style="color: #999;">暂未评价</div>
                                                </div>
                                            `}
                                        </div>
                                    </div>
                                `;
                                reviewsList.append(reviewItem);
                            });
                        } else {
                            showEmptyState('reviewsList', '暂无评价');
                        }
                    }
                }
            });
        }

        function getStarRating(rating) {
            let stars = '';
            for (let i = 0; i < 5; i++) {
                if (i < rating) {
                    stars += '<i class="fas fa-star"></i>';
                } else {
                    stars += '<i class="far fa-star"></i>';
                }
            }
            return stars;
        }

        function showEmptyState(containerId, message) {
            $(`#${containerId}`).html(`
                <div class="empty-state">
                    <i class="fas fa-box-open"></i>
                    <p>${message}</p>
                </div>
            `);
        }

        function startChat(targetUserId) {
            // 跳转到聊天页面
            location.href = `chat.html?targetUserId=${targetUserId}`;
        }
    </script>
</body>
</html> 